package it.mp.multitask.factory;

import it.mp.multitask.Main;
import it.mp.multitask.R;
import it.mp.multitask.Setup;
import it.mp.multitask.bean.App;
import it.mp.multitask.utility.AppComparator;
import it.mp.multitask.utility.IconManager;
import it.mp.multitask.utility.PatternEntry;
import it.mp.multitask.utility.RecentTaskInfoComparator;
import it.mp.multitask.utility.RunningServiceInfoComparator;
import it.mp.multitask.utility.RunningTaskInfoComparator;
import it.mp.multitask.view.HorizontalScrollViewTask;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;

import org.xmlpull.v1.XmlPullParser;

import android.app.ActivityManager;
import android.app.ActivityManager.RecentTaskInfo;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.BitmapDrawable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class StyleIphone implements Style {

	private SharedPreferences prefs;
	private BitmapDrawable bmpCestino = null;
	private Main main = null;
	private int dimensioneSchermo;
	private IconManager iconManager;
	private XmlPullParser xml;
	private HorizontalScrollViewTask hsv;
	private int sizeTask_Running = 0;
	private int sizeTask_Recent = 0;
	private int sizeTask_Preferred = 0;
	private static final int sizeSeparatore = 0;

	public StyleIphone(SharedPreferences prefs, BitmapDrawable bmpCestino, Main main, int dimensioneSchermo) {
		this.prefs = prefs;
		this.bmpCestino = bmpCestino;
		this.main = main;
		this.dimensioneSchermo = dimensioneSchermo;

	}

	public void crea(int NUMERO_PROCESSI_RUNNING, int NUMERO_PROCESSI_RECENTLY, int NUMERO_PROCESSI_SERVICE, int margin, boolean alphabeticOrder) {
		main.setContentView(R.layout.main_iphone);

		hsv = (HorizontalScrollViewTask) main.findViewById(R.id.HSV_contenitore);

		ActivityManager actvityManager = (ActivityManager) main.getSystemService(Main.ACTIVITY_SERVICE);

		final LinearLayout scrivania = (LinearLayout) main.findViewById(R.id.LL_contenitore);

		main.gestureDetector = new GestureDetector(main, new SimpleOnGestureListener());
		main.gestureDetector.setOnDoubleTapListener(new MyGestureDetector(prefs.getBoolean(main.getResources().getString(R.string.app_double_click), false)));

		final int patternRunning = android.R.color.transparent;
		final int patternService = android.R.color.transparent;
		final int patternRecently = android.R.color.transparent;
		final int patternPreferred = android.R.color.transparent;

		final LinearLayout llContent = (LinearLayout) main.findViewById(R.id.LL_contenitore_2);
		final int iconSize = Integer.valueOf(prefs.getString(main.getResources().getString(R.string.app_title_size), "56")).intValue();

		if (prefs.getBoolean(main.getResources().getString(R.string.audio_controls), false)) {

			ImageView iv = (ImageView) main.findViewById(R.id.IV_play);
			iv.setOnClickListener(main.oclPlay);
			iv.setImageResource(R.drawable.play_button);
			iv = (ImageView) main.findViewById(R.id.IV_prev);
			iv.setOnClickListener(main.oclPrev);
			iv = (ImageView) main.findViewById(R.id.IV_next);
			iv.setOnClickListener(main.oclNext);

		} else {
			llContent.removeView(main.findViewById(R.id.LL_title_4));
		}

		main.findViewById(R.id.LL_parent).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!prefs.getBoolean(main.getResources().getString(R.string.app_double_click), false)) {
					main.finish();
				}
			}
		});
		main.findViewById(R.id.LL_parent).setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				return main.gestureDetector.onTouchEvent(event);
			}
		});

		final LinearLayout llPreferred = (LinearLayout) main.findViewById(R.id.LL_task_0);
		final LinearLayout llRecently = (LinearLayout) main.findViewById(R.id.LL_task_1);
		final LinearLayout llRunning = (LinearLayout) main.findViewById(R.id.LL_task_2);
		final LinearLayout llService = (LinearLayout) main.findViewById(R.id.LL_task_3);
		final String scrollByGroup = prefs.getString(main.getResources().getString(R.string.theme_mode), "1");

		final boolean showDeleted = prefs.getBoolean(main.getResources().getString(R.string.app_show_app_deleted_by_system), false);

		final HashSet<String> hs = new HashSet<String>();
		hs.add("it.mp.multitask##it.mp.multitask.Main");
		hs.add("com.android.phone##com.android.phone.InCallScreen");

		final boolean showRecycle = prefs.getBoolean(main.getResources().getString(R.string.app_recycle_bin), true);
		final boolean dontShowTitle = true;

		iconManager = new IconManager(iconSize, bmpCestino);

		if (prefs.getBoolean(Main.PREF_APP_RUNNING, true)) {
			sizeTask_Running = getAndShowRunningApps(NUMERO_PROCESSI_RUNNING, actvityManager, hs, llRunning, margin, alphabeticOrder, scrollByGroup,
					showRecycle, showDeleted, dontShowTitle, iconSize);
			BitmapDrawable bd = PatternEntry.createDrawable(main.getResources(), patternRunning, 0);
			if (bd == null) {
				llRunning.setBackgroundResource(patternRunning);
			} else {
				llRunning.setBackgroundDrawable(bd);
			}
		} else {
			llContent.removeView(llRunning);
			llContent.removeView(main.findViewById(R.id.LL_title_2));
		}

		if (prefs.getBoolean(Main.PREF_APP_RECENTLY, true)) {
			sizeTask_Recent = getAndShowRecentlyClosedApps(NUMERO_PROCESSI_RECENTLY, llRecently, actvityManager, hs, margin, alphabeticOrder,
					(patternRunning == patternRecently) && (prefs.getBoolean(Main.PREF_APP_RUNNING, true)), scrollByGroup, dontShowTitle, iconSize);
			BitmapDrawable bd = PatternEntry.createDrawable(main.getResources(), patternRecently, 0);
			if (bd == null) {
				llRecently.setBackgroundResource(patternRecently);
			} else {
				llRecently.setBackgroundDrawable(bd);
			}
		} else {
			llContent.removeView(llPreferred);
			llContent.removeView(main.findViewById(R.id.LL_title_1));
		}

		sizeTask_Preferred = getAndShowPreferredApps(hs, llPreferred, margin, alphabeticOrder, (patternPreferred == patternRecently)
				&& ((prefs.getBoolean(Main.PREF_APP_RECENTLY, true)) || (prefs.getBoolean(Main.PREF_APP_RUNNING, true))), scrollByGroup, dontShowTitle,
				iconSize);
		if (sizeTask_Preferred == 0) {
			llContent.removeView(llPreferred);
			llContent.removeView(main.findViewById(R.id.LL_title_0));
		} else {
			BitmapDrawable bd = PatternEntry.createDrawable(main.getResources(), patternPreferred, 0);
			if (bd == null) {
				llPreferred.setBackgroundResource(patternPreferred);
			} else {
				llPreferred.setBackgroundDrawable(bd);
			}
		}

		if (prefs.getBoolean(Main.PREF_APP_SERVICE, false)) {
			getAndShowServiceApps(NUMERO_PROCESSI_SERVICE, llService, actvityManager, hs, margin, alphabeticOrder, (patternRunning == patternService),
					scrollByGroup, showRecycle, showDeleted, iconSize);
			BitmapDrawable bd = PatternEntry.createDrawable(main.getResources(), patternService, 0);
			if (bd == null) {
				llService.setBackgroundResource(patternService);
			} else {
				llService.setBackgroundDrawable(bd);
			}
		} else {
			llContent.removeView(llService);
			llContent.removeView(main.findViewById(R.id.LL_title_3));
		}

		setScroll(scrollByGroup);
		setGroupTitle(actvityManager);

		ViewGroup parent = llContent;
		parent.measure(MeasureSpec.EXACTLY, MeasureSpec.UNSPECIFIED);
		int heightParent = parent.getMeasuredHeight();

		String imgBackground = prefs.getString(main.getResources().getString(R.string.pref_background), PatternEntry.primoValore());

		BitmapDrawable desk = PatternEntry.createDrawable(main.getResources(), imgBackground, Style.LAYOUT_EXTENDED, heightParent, iconSize);

		if (desk == null) {
			scrivania.setBackgroundResource(PatternEntry.decodePattern(imgBackground, Style.LAYOUT_EXTENDED));
			((LinearLayout.LayoutParams) hsv.getLayoutParams()).leftMargin = 10;
			((LinearLayout.LayoutParams) hsv.getLayoutParams()).rightMargin = 10;
		} else {
			scrivania.setBackgroundDrawable(desk);
			((LinearLayout.LayoutParams) hsv.getLayoutParams()).leftMargin = 0;
			((LinearLayout.LayoutParams) hsv.getLayoutParams()).rightMargin = 0;
		}

	}
	private int getAndShowRunningApps(int numero, ActivityManager actvityManager, HashSet<String> hs, LinearLayout linearLayout, int margin,
			boolean alphabeticOrder, String firstGroup, boolean showRecycle, boolean showDeleted, boolean showTitle, int iconSize) {
		App app = null;
		int size;
		List<RunningTaskInfo> procInfos = actvityManager.getRunningTasks(numero);
		if (alphabeticOrder) {
			Collections.sort(procInfos, new RunningTaskInfoComparator(main.getPackageManager()));
		}

		size = procInfos.size();
		int numApp = 0;
		for (RunningTaskInfo rti : procInfos) {
			if ((rti.numRunning == 0) && (!showDeleted)) {
			} else {
				try {
					app = new App(main, rti, showRecycle, iconManager);
				} catch (NameNotFoundException e) {
				}
				if (app != null) {
					if (prefs.getBoolean(app.getPackageName(), false)) {

					} else {
						if (!hs.contains(app.toString())) {
							app.setOnClickListener(main.oclStartActivity);
							app.setOnLongClickListener(main.oclKillActivity);
							linearLayout.addView(app.getView(margin, bmpCestino, false, showTitle, iconSize, true, xml));
							numApp++;
							hs.add(app.toString());
						}
					}
				}
			}
		}

		int cost = (size == 0) ? 0 : (margin + iconSize + margin) * numApp + sizeSeparatore;
		if ("1".equals(firstGroup)) {
			setBlankSpace(linearLayout);
		} else {
			if (size == 1) {
				setBlankSpaceForFillParent(linearLayout);
				cost = linearLayout.getMeasuredWidth();
			}
		}
		return cost;
	}
	private int getAndShowRecentlyClosedApps(int numero, LinearLayout linearLayout, ActivityManager actvityManager, HashSet<String> hs, int margin,
			boolean alphabeticOrder, boolean impostaMargine, String firstGroup, boolean showTitle, int iconSize) {

		App app = null;
		int size;
		List<RecentTaskInfo> recInfos = actvityManager.getRecentTasks(numero + hs.size(), 0);
		if (alphabeticOrder) {
			Collections.sort(recInfos, new RecentTaskInfoComparator(main.getPackageManager()));
		}

		size = recInfos.size();
		int numApps = 0;
		for (RecentTaskInfo rti : recInfos) {

			try {
				app = new App(main, rti, iconManager);
			} catch (NameNotFoundException e) {

			}
			if (app != null) {
				if (prefs.getBoolean(app.getPackageName(), false)) {

				} else {
					if (!hs.contains(app.toString())) {
						app.setOnClickListener(main.oclStartActivity);

						linearLayout.addView(app.getView(margin, null, false, showTitle, iconSize, true, xml));
						numApps++;
						hs.add(app.toString());
					}
				}
			}
			if (numApps == numero) {
				break;
			}
		}

		if (impostaMargine) {
			linearLayout.addView(main.getLayoutInflater().inflate(R.layout.app_separatore, null));
		}
		int cost = (size == 0) ? 0 : (margin + iconSize + margin) * numApps + sizeSeparatore;
		if ("2".equals(firstGroup)) {
			setBlankSpace(linearLayout);
		} else {
			if (size == 1) {
				setBlankSpaceForFillParent(linearLayout);
				cost = linearLayout.getMeasuredWidth();
			}
		}
		return cost;
	}

	private int getAndShowPreferredApps(HashSet<String> hs, LinearLayout linearLayout, int margin, boolean alphabeticOrder, boolean impostaMargine,
			String firstGroup, boolean showTitle, int iconSize) {
		int size = 0;
		String lista = prefs.getString(main.getResources().getString(R.string.Apps_list_preferred), ";");
		StringTokenizer st = new StringTokenizer(lista, ";");
		App app = null;
		String chiave;
		HashSet<String> hsTmp = new HashSet<String>();
		if (true) {
			ArrayList<App> alAppo = new ArrayList<App>();
			while (st.hasMoreTokens()) {
				chiave = st.nextToken();
				if (prefs.getBoolean(chiave, false)) {
					chiave = chiave.replace(Setup.PREFIX_PREFERRED_APPS, "");
					try {
						app = new App(main, chiave, iconManager);
					} catch (NameNotFoundException e) {

					}
					if (app != null) {
						if (!hsTmp.contains(app.toString())) {
							alAppo.add(app);
							size++;
							hsTmp.add(app.toString());
						}
					}
				}
			}
			if (alAppo.size() > 0) {
				hs.addAll(hsTmp);
				Collections.sort(alAppo, new AppComparator(main.getPackageManager()));
				for (App appoggio : alAppo) {
					appoggio.setOnClickListener(main.oclStartActivity);
					linearLayout.addView(appoggio.getView(margin, null, false, showTitle, iconSize, true, xml));
				}
			}
		}
		if (impostaMargine) {
			linearLayout.addView(main.getLayoutInflater().inflate(R.layout.app_separatore, null));
		}
		int cost = (size == 0) ? 0 : (margin + iconSize + margin) * size + sizeSeparatore;
		if ((size > 0) && ("3".equals(firstGroup))) {
			setBlankSpace(linearLayout);
		} else {
			if (size > 0) {
				setBlankSpaceForFillParent(linearLayout);
				cost = linearLayout.getMeasuredWidth();
			}
		}
		return cost;
	}

	private void getAndShowServiceApps(int NUMERO_PROCESSI_SERVICE, LinearLayout linearLayout, ActivityManager actvityManager, HashSet<String> hs, int margin,
			boolean alphabeticOrder, boolean impostaMargine, String firstGroup, boolean showRecycle, boolean showDeleted, int iconSize) {
		if (impostaMargine) {
			linearLayout.addView(main.getLayoutInflater().inflate(R.layout.app_separatore, null));
		}

		App app;
		List<RunningServiceInfo> recInfos = actvityManager.getRunningServices(NUMERO_PROCESSI_SERVICE);
		if (alphabeticOrder) {
			Collections.sort(recInfos, new RunningServiceInfoComparator(main.getPackageManager()));
		}
		int numApps = 0;
		for (RunningServiceInfo rsi : recInfos) {
			if ((!rsi.started) && (!showDeleted)) {
			} else {
				app = new App(main, rsi, showRecycle, iconManager);
				if (prefs.getBoolean(app.getPackageName(), false)) {

				} else {
					if (!hs.contains(app.toString())) {
						app.setOnClickListener(main.oclStartOptionPanel);
						app.setOnLongClickListener(main.oclKillActivity);
						linearLayout.addView(app.getView(margin, bmpCestino, true, true, iconSize, true, xml));
						numApps++;
						hs.add(app.toString());

					}
				}
			}
		}
		if ("4".equals(firstGroup)) {
			setBlankSpace(linearLayout);
		}
	}

	private void setBlankSpace(ViewGroup linearLayout) {
		linearLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY);
		int width = linearLayout.getMeasuredWidth();
		int diff = dimensioneSchermo - width;
		if (diff > 0) {
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			lp.setMargins(diff, 0, 0, 0);
			ImageView v = new ImageView(main);
			v.setMinimumWidth(1);
			linearLayout.addView(v, -1, lp);
		} else {
			diff = 0;
		}
	}
	private void setBlankSpaceForFillParent(ViewGroup linearLayout) {
		ViewGroup parent = (ViewGroup) linearLayout.getParent();
		parent.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY);
		int widthParent = parent.getMeasuredWidth();
		int width = linearLayout.getMeasuredWidth();

		int diff = widthParent - width;
		if (diff > 0) {
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			lp.setMargins(diff, 0, 0, 0);
			ImageView v = new ImageView(main);
			v.setMinimumWidth(1);
			linearLayout.addView(v, -1, lp);
		} else {
			diff = 0;
		}
	}
	private void setGroupTitle(ActivityManager actvityManager) {
		boolean showTitle = prefs.getBoolean(main.getResources().getString(R.string.show_title), true);
		if (!showTitle) {
			View vista = null;
			vista = main.findViewById(R.id.tv_title_0);
			if (vista != null) {
				vista.setVisibility(View.INVISIBLE);
			}
			vista = main.findViewById(R.id.tv_title_1);
			if (vista != null) {
				vista.setVisibility(View.INVISIBLE);
			}
			vista = main.findViewById(R.id.tv_title_2);
			if (vista != null) {
				vista.setVisibility(View.INVISIBLE);
			}
			vista = main.findViewById(R.id.tv_title_3);
			if (vista != null) {
				vista.setVisibility(View.INVISIBLE);
			}
		} else {
			boolean showavailableMemory = prefs.getBoolean(main.getResources().getString(R.string.app_show_available_memory), false);
			boolean killAll = prefs.getBoolean("pref_kill_all", false);
			final TextView tv = ((TextView) main.findViewById(R.id.tv_title_2));
			if (showavailableMemory) {
				final ActivityManager.MemoryInfo mem = new ActivityManager.MemoryInfo();
				actvityManager.getMemoryInfo(mem);
				StringBuffer sb = new StringBuffer(" ");
				sb.append(main.getResources().getString(R.string.available));
				sb.append(": ");
				sb.append((int) mem.availMem / 1048576);
				sb.append(" Mb.");
				if (tv != null) {
					sb.insert(0, tv.getText());
					tv.setText(sb.toString());
				}
			}
			if (killAll) {
				tv.setOnClickListener(this.main.oclKillAllActivity);
			}
		}
	}
	private void setScroll(String scrollByGroup) {
		if ("1".equals(scrollByGroup)) {
			hsv.setScrollPosition(sizeTask_Preferred + sizeTask_Recent);
		} else if ("2".equals(scrollByGroup)) {
			hsv.setScrollPosition(sizeTask_Preferred);
		} else if ("3".equals(scrollByGroup)) {
			hsv.setScrollPosition(0);
		} else if ("4".equals(scrollByGroup)) {
			hsv.setScrollPosition(sizeTask_Preferred + sizeTask_Recent + sizeTask_Running);
		}
	}

	public class MyGestureDetector implements GestureDetector.OnDoubleTapListener {
		boolean isDoppioClick = false;
		public MyGestureDetector(boolean valore) {
			super();
			isDoppioClick = valore;
		}

		public boolean onDoubleTap(MotionEvent e) {
			if (isDoppioClick) {
				main.finish();
				return true;
			} else {
				return false;
			}
		}

		public boolean onDoubleTapEvent(MotionEvent e) {
			if (isDoppioClick) {
				main.finish();
				return true;
			} else {
				return false;
			}
		}

		public boolean onSingleTapConfirmed(MotionEvent e) {
			return false;
		}

	}

}
